package com.example.guardian.network;

import android.util.Log;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class PostImage {
    public static void sendOkHttpRequest(String address, String imagepath, okhttp3.Callback callback) {
        OkHttpClient Client = new OkHttpClient();
        //new一个File对象，并传入路径
        File file = new File(imagepath);
        MediaType mediaType = MediaType.parse("image/*");
        //这里是设置上传的类型为image,/*是不限制图片格式
        //RequestBody image = RequestBody.create(MediaType.parse("image/*"), file);
        //请求体
        if(file != null){
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    // 此处可添加上传 参数
                    // photoFile 表示上传参数名,logo.png 表示图片名字
                    .addFormDataPart("image", imagepath,
                            RequestBody.create(MediaType.parse("multipart/form-data"), file))//文件
                    .build();

            Request request = new Request.Builder()
                    .url(address)
                    .post(requestBody)
                    .build();
        Client.newCall(request).enqueue(callback);}

    }
}
